<?php
namespace app\admin\controller;
use app\admin\model;

class Term extends Base{
	public function __construct()
	{
		parent::__construct();
      
		$this->Term = D('Term');
		$this->Model = D('TermModel');
	}
/**
 *  term 列表有session，消除后会一直提示无权限，暂时不去继承AUTH
 */
	
	
	public function index()
	{
        $active['term'] = 1;
        $this->view->active = $active;
        $select_all_parent_id = M('term')->field('parent_id')->select();
	    $auth = new \org\Auth();
		$info = $auth->getGroups(session('id'));
        $access_id = M('authGroup')->where('id='.$info[0]['group_id'])->find();
		
		//怎加栏目权限
		 if(!in_array((int)session("id"),C("ADMINISTRATOR"))){
		$where['id']=array('in',$access_id['listrules']);
		 }
		$where['parent_id']=0;
		
		$parentterm = D('Term')->where($where)->select();
		$this->view->assign('parenterm',$parentterm);
		//怎加栏目权限 END
		
		$modelList = D('termModel')->getField('id,name');
		$this->view->assign("modelList",$modelList);
		
		return $this->view->fetch();
	}

	
    public function getchild($fetch = false,$parent_id = null)
    {

        $parent_id   = is_null($parent_id) ? I('post.parent_id',"0","int") : $parent_id;
        //$this->view->depth = $parent_id == 0 ?  array() : $this->Term->depth($parent_id);
        $this->view->parenterm = $this->Term->getList(['parent_id'=>$parent_id]);
        $this->view->modelList = D('termModel')->getField('id,name');
        $content = $this->view->fetch();
        if($fetch){
            return  $content;
        }else {
            echo $content;
        }


    }
	
	public function addTerm($parent_id=1,$model_id=1)
	{
	
        $active['addTerm'] = 1;
        $this->view->active = $active;
		$all_term =$this->Term->selectAll();
		$fen_ji = $this->getTermTree(0,$all_term);
		$this->view->assign("all_term",$fen_ji);



        //获取自定义标签
        $termInfo = $this->Term->getTermInfo($parent_id);
        $termModelModel = D('TermModel');
        $tagList = $termModelModel->getIdModelTags($termInfo['model_id'], 'term');
        $formCon = A('Form');
        $formList = $formCon->createForm($tagList, json_decode($termInfo['tags_value'],1), 'tag');
		$this->view->assign('formList',$formList);
		
        $modelList = D('termModel')->getField('id,name');
		if(empty($modelList)) $modelList = [];
		$this->view->assign("modelList",$modelList);


		if(IS_POST){
			$tagsValue = A('Form')->getValue('tag');
            if(0==I('post.model_id')){
			    return $this->error('请选择模型');
			}
            $info_addterm = $this->Term->addTerm($tagsValue);
		    if($info_addterm){
			    return $this->success('添加成功', U('Term/index') );
		    }else{
			    return $this->error('添加失败', U('Term/index'));
		    }
		}

		return $this->view->fetch();
	}
	
	
	public function editTerm($id)
	{
        $active['term'] = 1;
        $this->view->active = $active;
		//栏目选择
		$all_term =$this->Term->selectAll();
		$fen_ji = $this->getTermTree(0,$all_term);
		$this->view->assign("all_term",$fen_ji);
		
		//显示所编辑内容
		$termInfo = $this->Term->getTermInfo($id);
		$this->view->assign('edit_info',$termInfo);


        //获取自定义标签
        $termModelModel = D('TermModel');
        $tagList = $termModelModel->getIdModelTags($termInfo['model_id'], 'term');
        $formCon = A('Form');
        $formList = $formCon->createForm($tagList, json_decode($termInfo['tags_value'],1), 'tag');
		$this->view->assign('formList',$formList);
		
        $modelList = D('termModel')->getField('id,name');
		if(empty($modelList)) $modelList = [];
		$this->view->assign("modelList",$modelList);
		
		return $this->view->fetch();
	}
	
    public function postEdit(){
        $tagsValue = A('Form')->getValue('tag');

		$post_edit = $this->Term->postEdit($tagsValue);
		if($post_edit !=="false"){
			manage_log_success();
			return $this->success('编辑成功', U('Term/index'));
		}else{
			manage_log_error();
			return $this->error('编辑失败', U('Term/index'));
		}
		
	}
	
	public function delTerm($id){
	 
	   $reconfirm = $this->reconfirm('删除后无法恢复，是否确认删除？');
        if(!$reconfirm){
			 return $this->error('取消删除', U('Term/index'));
		}
		$del_term = $this->Term->delTerm($id);
		 if($del_term){
			 manage_log_success();
			 return $this->success('删除成功', U('Term/index'));
		   }else{
			   manage_log_error();
			 return $this->error('删除失败', U('Term/index'));
		   }
	}
	

	
	public function modelSelect()
	{
		$model_select =$this->Model->selectModel();
		$this->view->assign('model_select',$model_select);
	}
	
	public function listorder(){
		
		$status = $this->Term->listorder($_POST['listorder']);
		 if($status){
			 manage_log_success();
			 return $this->success('排序成功', U('Term/index'));
		 }else{
			  manage_log_error();
			 return $this->error('排序失败', U('Term/index'));
		 }
		
	}
	
	
	
	
	
}
